import type { BaseModelConstructor, DataKey, ModelParams } from "../../types";
import { IBaseModel } from "../../types";
import { setModelState } from "../../hooks/useModelOptions";


export const useModelDescriptor = <T extends IBaseModel<T>>() => {
  const Model = (parmas: DataKey | ModelParams<T>) => {
    return <Class extends BaseModelConstructor>(value: Class): BaseModelConstructor<T> => {
      // if (!parmas) {
      //   parmas = value.name
      // }
      if (typeof parmas === 'string' || typeof parmas === 'symbol') {
        // 初始化$OPTS如果不存在
        if (!value.$OPTS) {
          value.$OPTS = {} as any
        }
        if (!value.$OPTS!.modelKey) {
          value.$OPTS!.modelKey = parmas
        }
        setModelState(value, {
          name: parmas.toString()
        })
      } else {
        // 初始化$OPTS如果不存在
        if (!value.$OPTS) {
          value.$OPTS = {} as any
        }
        if (!value.$OPTS!.modelKey && parmas.name) {
          value.$OPTS!.modelKey = parmas.name
        }
        setModelState(value, parmas as any)
      }

      return value as unknown as BaseModelConstructor<T>
    }
  }
  return Model
}

// export const Model = <T extends IBaseModel<T>>(parmas: DataKey | ModelParams<T>) => {
//   return <Class extends BaseModelConstructor>(value: Class): BaseModelConstructor<T> => {
//     // if (!parmas) {
//     //   parmas = value.name
//     // }
//     if (typeof parmas === 'string' || typeof parmas === 'symbol') {
//       // 初始化$OPTS如果不存在
//       if (!value.$OPTS) {
//         value.$OPTS = {} as any
//       }
//       if (!value.$OPTS!.modelKey) {
//         value.$OPTS!.modelKey = parmas
//       }
//       setModelState(value, {
//         name: parmas.toString()
//       })
//     } else {
//       // 初始化$OPTS如果不存在
//       if (!value.$OPTS) {
//         value.$OPTS = {} as any
//       }
//       if (!value.$OPTS!.modelKey && parmas.name) {
//         value.$OPTS!.modelKey = parmas.name
//       }
//       setModelState(value, parmas as any)
//     }

//     return value as unknown as BaseModelConstructor<T>
//   }
// }

export { setModelState }